package view;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.wb.swt.SWTResourceManager;

import com.paziy.calc.Calculator;
import com.paziy.calc.operations.*;

public class Window {

	protected Shell shell;
	private Text text;
	private final Font tnr15 = SWTResourceManager.getFont("Times New Roman", 15, SWT.NORMAL);
	
	private double op1, op2;
	private Operation oper;
	
	private final Operation add;
	private final Operation sub;
	private final Operation mul;
	private final Operation div;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Window window = new Window();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public Window() {
		add = new Addition();
		sub = new Subtraction();
		mul = new Multiplication();
		div = new Division();
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		
		tnr15.dispose();
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {		
		
		shell = new Shell(SWT.DIALOG_TRIM);
		shell.setMinimumSize(new Point(300, 300));
		shell.setSize(300, 300);
		shell.setText("SWT Application");
		
		Menu menu = new Menu(shell, SWT.BAR);
		shell.setMenuBar(menu);
		
		MenuItem mntmNewWindow = new MenuItem(menu, SWT.NONE);
		mntmNewWindow.setText("New window");
		
		text = new Text(shell, SWT.BORDER | SWT.RIGHT);
		text.setFont(SWTResourceManager.getFont("Times New Roman", 15, SWT.NORMAL));
		text.setEditable(false);
		text.setBounds(10, 10, 274, 42);
		
		Button btnNewButton = new Button(shell, SWT.NONE);
		btnNewButton.setFont(tnr15);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				text.setText(text.getText() + "7");
			}
		});
		btnNewButton.setBounds(10, 58, 45, 45);
		btnNewButton.setText("7");
		
		Button btnNewButton_1 = new Button(shell, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				text.setText(text.getText() + "8");
			}
		});
		btnNewButton_1.setFont(tnr15);
		btnNewButton_1.setBounds(65, 58, 45, 45);
		btnNewButton_1.setText("8");
		
		Button btnNewButton_2 = new Button(shell, SWT.NONE);
		btnNewButton_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				text.setText(text.getText() + "6");
			}
		});
		btnNewButton_2.setFont(tnr15);
		btnNewButton_2.setBounds(116, 109, 45, 45);
		btnNewButton_2.setText("6");
		
		Button btnNewButton_3 = new Button(shell, SWT.NONE);
		btnNewButton_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				text.setText(text.getText() + "9");
			}
		});
		btnNewButton_3.setFont(tnr15);
		btnNewButton_3.setBounds(116, 58, 45, 45);
		btnNewButton_3.setText("9");
		
		Button btnNewButton_4 = new Button(shell, SWT.NONE);
		btnNewButton_4.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				text.setText(text.getText() + "4");
			}
		});
		btnNewButton_4.setFont(tnr15);
		btnNewButton_4.setBounds(10, 109, 45, 45);
		btnNewButton_4.setText("4");
		
		Button btnNewButton_5 = new Button(shell, SWT.NONE);
		btnNewButton_5.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				text.setText(text.getText() + "5");
			}
		});
		btnNewButton_5.setFont(tnr15);
		btnNewButton_5.setBounds(65, 109, 45, 45);
		btnNewButton_5.setText("5");
		
		Button btnNewButton_6 = new Button(shell, SWT.NONE);
		btnNewButton_6.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				text.setText(text.getText() + "1");
			}
		});
		btnNewButton_6.setFont(tnr15);
		btnNewButton_6.setBounds(10, 160, 45, 45);
		btnNewButton_6.setText("1");
		
		Button btnNewButton_7 = new Button(shell, SWT.NONE);
		btnNewButton_7.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				text.setText(text.getText() + "2");
			}
		});
		btnNewButton_7.setFont(tnr15);
		btnNewButton_7.setBounds(65, 160, 45, 45);
		btnNewButton_7.setText("2");
		
		Button btnNewButton_8 = new Button(shell, SWT.NONE);
		btnNewButton_8.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				text.setText(text.getText() + ".");
			}
		});
		btnNewButton_8.setFont(tnr15);
		btnNewButton_8.setBounds(116, 211, 45, 35);
		btnNewButton_8.setText(".");
		
		Button btnNewButton_9 = new Button(shell, SWT.NONE);
		btnNewButton_9.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				text.setText(text.getText() + "0");
			}
		});
		btnNewButton_9.setFont(tnr15);
		btnNewButton_9.setBounds(10, 211, 100, 35);
		btnNewButton_9.setText("0");
		
		Button btnNewButton_10 = new Button(shell, SWT.NONE);
		btnNewButton_10.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				text.setText(text.getText() + "3");
			}
		});
		btnNewButton_10.setFont(tnr15);
		btnNewButton_10.setBounds(116, 160, 45, 45);
		btnNewButton_10.setText("3");
		
		Button btnNewButton_11 = new Button(shell, SWT.NONE);
		btnNewButton_11.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				op1 = Double.valueOf(text.getText());
				oper = div;
				text.setText("");
			}
		});
		btnNewButton_11.setFont(tnr15);
		btnNewButton_11.setBounds(167, 58, 45, 45);
		btnNewButton_11.setText("/");
		
		Button btnNewButton_12 = new Button(shell, SWT.NONE);
		btnNewButton_12.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				op1 = Double.valueOf(text.getText());
				oper = mul;
				text.setText("");
			}
		});
		btnNewButton_12.setFont(tnr15);
		btnNewButton_12.setBounds(167, 109, 45, 45);
		btnNewButton_12.setText("*");
		
		Button btnNewButton_13 = new Button(shell, SWT.NONE);
		btnNewButton_13.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				op1 = Double.valueOf(text.getText());
				oper = sub;
				text.setText("");
			}
		});
		btnNewButton_13.setFont(tnr15);
		btnNewButton_13.setBounds(167, 160, 45, 45);
		btnNewButton_13.setText("-");
		
		Button btnNewButton_14 = new Button(shell, SWT.NONE);
		btnNewButton_14.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				op1 = Double.valueOf(text.getText());
				oper = add;
				text.setText("");
			}
		});
		btnNewButton_14.setFont(tnr15);
		btnNewButton_14.setBounds(167, 211, 45, 35);
		btnNewButton_14.setText("+");
		
		Button btnNewButton_15 = new Button(shell, SWT.NONE);
		btnNewButton_15.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				op2 = Double.valueOf(text.getText());
				text.setText(String.valueOf(Calculator.calculate(oper, op1, op2)));
			}
		});
		btnNewButton_15.setFont(tnr15);
		btnNewButton_15.setBounds(218, 58, 66, 147);
		btnNewButton_15.setText("=");
		
		Button btnC = new Button(shell, SWT.NONE);
		btnC.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				text.setText("");
			}
		});
		btnC.setBounds(218, 211, 66, 35);
		btnC.setText("C");

	}
}
